package com.zdkj.subway.check.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdkj.subway.check.entity.SwAuConductorNotice;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuConductorNoticeMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.result.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;


import javax.annotation.Resource;
import java.util.*;
/**
 * 线网层级确认-售票员票款差异确认-售票员票款差异确认通知
 * SW_AU_CONDUCTOR_NOTICE
 */
@Service
public class SwAuConductorNoticeService {

    @Resource
    private SwAuConductorNoticeMapper swAuConductorNoticeMapper;
	@Resource
    private SysUserMapper sysUserMapper;

    /**
     * 售票员票款差异通知 列表
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNoticeList(JSONObject jsonObject) {
        //售票员id
        Integer conductorPersonId = jsonObject.getInteger("conductorPersonId");
        String conductorPersonName = jsonObject.getString("conductorPersonName");//售票员姓名
        String isDifferenceAmount = jsonObject.getString("isDifferenceAmount");//长短款标识
        Integer sendStatus = jsonObject.getInteger("sendStatus");
        //线路站点
        String lineCode = jsonObject.getString("lineCode");
        String stationCode = jsonObject.getString("stationCode");
        //申诉状态
        String appealStatus = jsonObject.getString("appealStatus");
        //收入时间
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");

        //申诉时间
        Date startSendTime = jsonObject.getDate("startSendTime");
        Date endSendTime = jsonObject.getDate("endSendTime");
        //分页
        Integer startPage = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");
        Example example = new Example(SwAuConductorNotice.class);
        example.setOrderByClause("RECORD_TIME desc");
        Example.Criteria criteria = example.createCriteria();
        if (conductorPersonId != null) {
            criteria.andEqualTo("conductorPersonId", conductorPersonId);
        }
        if (!StringUtils.isEmpty(conductorPersonName)) {
            criteria.andLike("conductorPersonName", conductorPersonName);
        }
        if (!StringUtils.isEmpty(appealStatus)) {
            criteria.andEqualTo("appealStatus", appealStatus);
        }
        if (!StringUtils.isEmpty(sendStatus)) {
            criteria.andEqualTo("sendStatus", sendStatus);
        }
        if (startTime != null && endTime != null) {
            criteria.andBetween("recordTime",startTime,endTime);
        }
        if (startSendTime != null && endSendTime != null) {
            criteria.andBetween("sendTime",startSendTime,endSendTime);
        }
        if (!StringUtils.isEmpty(lineCode)) {
            criteria.andEqualTo("lineCode", lineCode);
        }
        if (!StringUtils.isEmpty(stationCode)) {
            criteria.andEqualTo("stationCode", stationCode);
        }
	    if(StringUtils.isEmpty(lineCode)) {
            PigxUser user = SecurityUtils.getUser();
            Map<String, Object> p1 = new HashMap<>();
            p1.put("user_code", user.getUserCode());
            List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
            SysUser sysUser = sysUsers.get(0);
            String managementLineStation = sysUser.getManagementLineStation();
            //管辖线路
            if (!StringUtils.isEmpty(managementLineStation)) {
                JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
                List<String> LineObjects = new ArrayList<>();
                for (int i = 0; i < managementLineStations.size(); i++) {
                    JSONObject jsonObject1 = managementLineStations.getJSONObject(i);
                    lineCode = jsonObject1.getString("line");
                    LineObjects.add(lineCode);
                }
                criteria.andIn("lineCode", LineObjects);
            }else {
                //不管辖线路
                criteria.andEqualTo("lineCode", sysUser.getLineCode());
            }
        }
        if (!StringUtils.isEmpty(isDifferenceAmount)) {
            if("1".equals(isDifferenceAmount)){
                criteria.andGreaterThan("differenceAmount", 0);
            }else {
                criteria.andLessThan("differenceAmount", 0);
            }
        }
        PageHelper.startPage(startPage,pageSize);
        List<SwAuConductorNotice> swAuConductorNotices = swAuConductorNoticeMapper.selectByExample(example);

        PageInfo<SwAuConductorNotice> swAuConductorNoticePageInfo = new PageInfo<>(swAuConductorNotices);
        JSONObject resultJson = new JSONObject();
        resultJson.put("list",swAuConductorNotices);
        resultJson.put("pageInfo",swAuConductorNoticePageInfo);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    /**
     * 售票员票款差异通知 单独查询
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNotice(JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        SwAuConductorNotice swAuConductorNotice = swAuConductorNoticeMapper.selectByPrimaryKey(id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("obj",swAuConductorNotice);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    /**
     * 售票员票款差异通知 申诉
     * @param jsonObject
     * @return
     */
    public Result updateSwAuConductorNotice(JSONObject jsonObject) {
        SwAuConductorNotice swAuConductorNotice =
                JSONObject.parseObject(jsonObject.toJSONString(), SwAuConductorNotice.class);
        int i = swAuConductorNoticeMapper.updateById(swAuConductorNotice);
        return new Result(ResultDetailMsg.OK);
    }
}
